/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//dsa
package testeinterface;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class PCertificado extends JPanel{	       
        
        public JButton btInserir;
        
        public PCertificado(){            
            this.setLayout(new GridLayout(4,4));
            
            final JTextField tfCodigo = new JTextField("",20);
            final JTextField tfNome   = new JTextField("",20);            
            final JTextField tfDescricao   = new JTextField("",20);
            
           JLabel lbCodigo       = new JLabel("Código do certificado");
           JLabel lbNome         = new JLabel("Nome do certificado");
           JLabel lbDescricao    = new JLabel("Descricao");
            
            btInserir      = new JButton("Inserir");
            
            btInserir.addActionListener (new ActionListener () {  
            @Override
                 public void  actionPerformed(ActionEvent e){  
                     
                     Logger.gravar(Logger.INICIALIZANDO_METODO, getNomeClasse() + ".actionPerformed(ActionEvent e)");
                     
                     try{
                        String host = "jdbc:mysql://143.106.150.216:3306/SatodaDB?zeroDateTimeBehavior=convertToNull";
                        String uName = "satoda";
                        String uPass = "adotas2014";
                        Connection con = DriverManager.getConnection(host, uName, uPass);
                        
                        Statement stmt = con.createStatement( );
                        String SQL = "INSERT INTO T_CERTIFICADO(codigo, nome, descricao) VALUES ("+ tfCodigo.getText() +", '"+ tfNome.getText() +"', '"+tfDescricao.getText()+"')";
                        stmt.executeUpdate( SQL );
                    }
                    catch( SQLException err ){
                        Logger.gravar(Logger.ERRO, getNomeClasse() + ".actionPerformed(ActionEvent e): " + err.getMessage());
                        System.out.println(err.getMessage());
                    }
                    
                    Logger.gravar(Logger.FINALIZANDO_METODO, getNomeClasse() + ".actionPerformed(ActionEvent e)");
                    System.out.println("Certificado cadastrado com sucesso!");  
                 }  
              });  



            
            this.add(lbCodigo);
            this.add(tfCodigo);
            
            this.add(lbNome);
            this.add(tfNome);
            
            this.add(lbDescricao);
            this.add(tfDescricao);
            this.add(btInserir);
            
            
        }
        
        private String getNomeClasse(){
            return this.getClass().getName();
        }
 
}

